package com.emin.platform.subjectcenter.common.response;

import com.emin.base.exception.EminException;
import com.emin.platform.subjectcenter.exception.ExceptionCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 返回结构体
 *
 * @author Jack
 * @version V1.0
 * @Title:
 * @Description: 用于接口返回
 * @date 2017/10/25 9:58
 */
@Data
public class ResponseBack<T> implements Serializable {

    private static final String SUCCESS_CODE = "ok";

    private static final long serialVersionUID = 2843894848383050315L;
    private boolean success;
    private String code;
    private T result;


    public static final <T> ResponseBack<T> success(T t) {
        return new ResponseBack("ok", t);
    }

    private ResponseBack(String code, T result) {
        this.code = code;
        this.result = result;
        this.success = this.checkIsSuccess();
    }

    private boolean checkIsSuccess() {
        return this.code.equals(SUCCESS_CODE) ? true : false;
    }


    public static final <T> ResponseBack<T> error(EminException e) {
        return new ResponseBack(e);
    }

    public ResponseBack(EminException e) {
        this.code = e.getCode();
        this.result = null;
    }

    public static final <T> ResponseBack<T> error(ExceptionCode systemError) {
        return new ResponseBack(systemError);
    }

    public ResponseBack(ExceptionCode systemError) {
        this.code = systemError.getCode();
        this.result = null;
    }

}
